#include <cstdio>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

#include "common.h"
#include <iostream>
#include <chrono>
using namespace std;

#define handle_error(msg) do{ perror(msg); exit(0);}while(0);


int main()
{

	//create
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	/*int val = 1;
	int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void*)&val, sizeof(val));
	if (ret < 0)handle_error("set reuseaddr error");
	ret = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (const void*)&val, sizeof(val));
	if (ret < 0)handle_error("set reuseport error");*/

	if (fd < 0)handle_error("create socket fd error: ");
	struct sockaddr_in addr { 0 };
	addr.sin_port = htons(9000);
	addr.sin_family = AF_INET;
	inet_pton(AF_INET, "localhost", &addr.sin_addr);
	//addr.sin_addr.s_addr = inet_addr("localhost");
	//bind
	
	int ret = connect(fd, (const sockaddr*)&addr, sizeof(addr));
	if (ret < 0)
	{
		handle_error("connect error");
	}
	cout << "connect success!\n";
	char buf[8] = {0x00,0x00,0x04,0x00, 0x00,0x00,0x20,0x00};
	char buff[8192] = { 0 };
	send(fd, buf, 8, 0);
	while (1)
	{
		int rett = send(fd, buff, 8192, 0);
		if (rett < 8192)handle_error("send error: ");
	}



	return 0;
}